<?php
$caption = BUtils::T('preview_btn');
$this->pageTitle = Yii::app()->name . ' - ' . $caption;

$edit_btn = BUtils::T('edit_btn');
$edit_profile_btn = BUtils::T('edit_profile_btn');

//get url_logo
$url_logo = empty($model->url_logo) ? Yii::app()->request->baseUrl . '/images/logo_default.png' : $model->url_logo;

//set $full_lanlng
$full_lanlng = '';
if (!empty($model->latitude) and !empty($model->longitude)) {
	$full_lanlng = "[{$model->latitude}, {$model->longitude}]";
}

//get week=?
$request_week_num = Yii::app()->request->getParam('week', date('N'));
$today_week_num = date('N');
$inc_days = $request_week_num - $today_week_num;
$visit_date_ts = mktime(0, 0, 0, date('m'), date('d') + $inc_days);
$visit_date = date('d.m.Y', $visit_date_ts);

$visit_week = date('l', $visit_date_ts);
$visit_week_html = BUtils::T(strtolower($visit_week) . '_lbl');

//create calendar for week days
$html_week_days_link = '';
for ($i = 1; $i < 8; $i ++) {
	if ($request_week_num == $i) {
		$url = 'javascript:;';
		$style = " w{$i} active";
	} else {
		$url = Yii::app()->createUrl($this->id.'/'.$this->action->id, array('week'=>$i));
		$style = " w{$i}";
	}
	//set date timestamp
	$diff = $i - $today_week_num;
	$date_ts = mktime(0, 0, 0, date('m'), date('d') + $diff);
	$html_week_days_link .= '<div class="calendar'.$style.'">
		<a href="'.$url.'">
			<div class="date">'.date('M j', $date_ts).'</div>
			<div class="week">'.date('D', $date_ts).'</div>
		</a>
</div>';
}

//define array
$aWeekDaysShort = BUtils::WeekDayKeys(7, true);
$aOpenningHours = BUtils::AllHour24();

//set open hours
$hours_list = '';
if (!empty($hours)) {
	//set open hours display style
	$sWeekBuffer = array();
	foreach ($hours as $v) {
		$sWeekday = $aWeekDaysShort[$v['week_no']];
		$sHours = $aOpenningHours[$v['open_hour']] . ' - ' . $aOpenningHours[$v['close_hour']];
		$sWeekBuffer[$sHours][] = $v['week_no'];//add the current week day
	}
	foreach ($sWeekBuffer as $key => $v) {
		$size = count($v);
		if ($size > 1) $sWeekday = $aWeekDaysShort[$v[0]] . ' - ' . $aWeekDaysShort[$v[$size-1]];
		else $sWeekday = $aWeekDaysShort[$v[0]];
		$hours_list .= '<div class="item"><span class="weekday">'.$sWeekday.'</span><span class="hours">'.$key.'</span></div>';
	}
}
?>
<div class="container prepend-top append-bottom">
	<div class="span-14">
		<div class="box_white">
			<div class="calendar-fields">
				<?php echo $html_week_days_link; ?>
				<div class="clear"></div>
			</div>
			
			<div class="biz-form">
				<h3><?php echo BUtils::T('available_specials_lbl'); ?></h3>
				<div class="tips"><?php echo BUtils::T('available_specials_eg'); ?></div>
				<div class="merchant-field">
					<div class="fieldswrap">
						<div class="caption-line relative">
							<div class="fixed-title">
								<div>
									<div class="span-5"><?php echo ($inc_days===0) ? BUtils::T('today_special_lbl') : BUtils::T('special_lbl'); ?></div>
									<div class="left">
										<span class="space-1"><?php echo $visit_date; ?></span>
										<span class="space-1"><?php echo $visit_week_html; ?></span>
									</div>
									<div class="clear"></div>
								</div>
							</div>
							<div class="corner-tr edit-button">
								<!-- <a class="button button-small button-confirm" href="<?php echo Yii::app()->createUrl('/special'); ?>"><span><?php echo $edit_btn; ?></span></a> -->
							</div>
						</div>
						<div>
<?php if (count($special_rows) > 0) {
	$predefined_days = BUtils::WeekDayKeys(7, true);
foreach ($special_rows as $k => $r) {
	//set offer picture
	$url_thumb_default = Yii::app()->baseUrl . '/images/default_special_thumb.png';
	$url_picture_default = Yii::app()->baseUrl . '/images/default_special_picture.png';
	$url_thumb = empty($r['url_picture']) ? $model->url_offer_img : $r['url_picture'];
	if (!empty($url_thumb)) {
		$url_thumb = $url_thumb;
		$url_picture = str_replace('thumb_', '', $url_thumb);
	} else {
		$url_thumb = $url_thumb_default;
		$url_picture = $url_picture_default;
	}
	
	//offer valid time
	$range_time = BUtils::GetHourRangeFormat($r['start_hour'], $r['end_hour']);
	//offer valid date
	$range_week = BUtils::GetWeekdays($r['week_days'], $predefined_days);
	
	//set price, discount or highlight
	$highlight = $del_price = '';
	if (!empty($r['price'])) {
		$highlight = MerchantObject::formatPrice($r['price']);
		if (!empty($r['value'])) {
			$del_price = '<span class="oldprice label">€'.$r['value'].'</span>';
		}
	} elseif (!empty($r['discount'])) {
		$highlight = MerchantObject::formatDiscount($r['discount']);
	}
	
	//offer content
	$content = strip_tags(trim($r['content']));
	$content = strlen($content) > 72 ? substr($content, 0, 72) . '...' : $content;
?>
<div class="mb_offer" style="padding:8px 0; border-bottom:1px solid #ddd;">
	<div class="mb_picture">
		<a href="<?php echo Yii::app()->createUrl('/special/preview', array('id'=>$r['id'])) ?>" class="popup-btn">
			<img src="<?php echo $url_thumb; ?>" alt="" onError="this.onerror=null;this.src='<?php echo $url_thumb_default; ?>';" />
		</a>
	</div>
	<div class="mb_price" style="width:100px;">
		<div class="highlight"><?php echo $highlight; ?></div>
		<?php echo $del_price; ?>
	</div>
	<div class="mb_period">
		<div class="time"><?php echo $range_time; ?></div>
		<div class="date"><?php echo $range_week; ?></div>
	</div>
	<div class="mb_text" style="margin:0;">
		<?php echo $content; ?>
	</div>
	<div class="clearfix clear"></div>
</div>
<?php
}
?>
<?php } ?>
						</div>
					</div>
				</div>
				<div class="clear"></div>
			</div>
		</div>
	</div>
	
	<div class="span-10 right last">
		<div class="box_white">
			<div class="biz-form">
				<h3 title="<?php echo Yii::app()->user->id; ?>"><?php echo $model->name; ?></h3>
				<div class="fieldswrap relative">
					<div style="position:absolute; right:5px; top:5px;">
						<a class="button button-small button-confirm" href="<?php echo Yii::app()->createUrl('/merchant/profile'); ?>" title="<?php echo $edit_profile_btn; ?>"><span><?php echo $edit_profile_btn; ?></span></a>
					</div>
					<div class="fields mobile">
						<div class="left" style="margin-right:10px;">
							<a class="pic"><img src="<?php echo $url_logo; ?>" width="100" /></a><br />
							<a href="http://yamii.fi/place/<?php echo Yii::app()->user->id; ?>" target="_blank"><?php echo BUtils::T('view_your_page_lbl'); ?></a>
						</div>
						<div style="overflow:hidden; margin-top:20px;">
							<input type="hidden" id="geo-auto-location" />
							<div class="address">
								<div><?php echo BUtils::GetAddress($model->address); ?></div>
								<div><?php echo $model->biz_area; ?></div>
								<div><?php echo BUtils::GetZipCity($model->postcode, $model->city); ?></div>
							</div>
							<div class="phone"><?php echo $model->phone; ?></div>
							<div class="link"><?php echo $model->url_site; ?></div>
						</div>
						<div class="clear"></div>
					</div>
					<div class="fields mobile">
						<div id="merchant-map" class="map-fields">
							<img src="<?php echo Yii::app()->baseUrl; ?>/images/loader-bar.gif" />
						</div>
					</div>
				</div>
				<div class="fieldswrap">
					<div class="fields mobile">
						<div class="fixed-title"><?php echo BUtils::T('opening_hours_lbl'); ?></div>
						<div class="added-hours"><?php echo $hours_list; ?></div>
					</div>
				</div>
				<div class="fieldswrap relative">
					<div style="position:absolute; right:5px; top:5px;">
						<a class="button button-small button-confirm" href="<?php echo Yii::app()->createUrl('/merchant/feature'); ?>"><span><?php echo $edit_btn; ?></span></a>
					</div>
					<div class="fields mobile">
						<div class="fixed-title"><?php echo BUtils::T('detail_features_lbl'); ?></div>
						<div class="tags">
<?php
if (!empty($tags)) {
	foreach ($tags as $key => $value) {
		if (!empty($value)) {
			$string = null;
			$array = explode(',', $value);
			foreach ($array as $item) {
				$string .= '<span class="nature-list">' . BUtils::T($item) . '</span>';
			}
			echo '<div style="font-weight:bold;margin-top:10px;">'.BUtils::T($key . '_lbl').':</div><div style="margin-left:40px;">'.$string.'</div>';
		}
	}
}
?>
						</div>
					</div>
				</div>
<?php if (!empty($model->brief)) { ?>
				<div class="fieldswrap">
					<div class="fields mobile">
						<div class="fixed-title"><?php echo BUtils::T('merchant_brief_lbl'); ?></div>
						<div class="opening">
							<p><?php echo nl2br($model->brief); ?></p>
						</div>
					</div>
				</div>
<?php } ?>
			</div>
		</div>
	</div>
</div>
<?php
//get the latLng
if (empty($model->latitude) or empty($model->longitude)) {
	$latitude = 'null';
	$longitude = 'null';
	$address = "'{$model->address}'";
} else {
	$latitude = $model->latitude;
	$longitude = $model->longitude;
	$address = 'null';
}
?>
<style type="text/css">
.map-fields{
	margin: 0px auto;
	width: 100%;
	text-align: center;
	vertical-align: middle;
}
</style>
<script type="text/javascript">
$(function() {
	//load gMap
	$('#merchant-map').css('height', '200px');
	//map marker
	var imageIcon = new google.maps.MarkerImage('<?php echo Yii::app()->baseUrl . "/images/markers/pin_{$model->category_key}.png"; ?>',
		new google.maps.Size(32, 37), new google.maps.Point(0,0), new google.maps.Point(16,37));
	var imageShadow = new google.maps.MarkerImage('<?php echo Yii::app()->baseUrl; ?>/images/markers/pin_shadow.png',
		new google.maps.Size(51, 37), new google.maps.Point(0,0), new google.maps.Point(16,37));
	$("#geo-auto-location").geocomplete({
		map: '#merchant-map',
		<?php if(!empty($full_lanlng)) echo "location: {$full_lanlng},"; ?>
		markerOptions: {
			icon: imageIcon,
			shadow: imageShadow
		}
	});
	
	//open a menu picture
	$('.menu-pictures a').fancybox({autoScale:false,transitionIn:'elastic',transitionOut:'elastic'});

	//open a special box
	$('.preview-btn, .popup-btn, #show-bonus-detail').facebox({
		loadingImage: '<?php echo Yii::app()->baseUrl ?>/images/loader-bar.gif',
		closeImage: '<?php echo Yii::app()->baseUrl ?>/images/btn-close.png',
		overlayClose:false,
		width: 360
	});
});

function beforeSend() {
	$("#loading-bar").show();
	$("#item-list").html('');
}
function success(html) {
	$("#loading-bar").hide();
	$("#item-list").html(html);
}

function ajaxPages(url) {
	jQuery.ajax({
		'type':'POST',
		'url':url,
		'cache':false,
		'data':jQuery(this).parents("form").serialize(),
		'beforeSend':beforeSend,
		'success':success
	});
}
</script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places&language=<?php echo Yii::app()->language; ?>"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/labs/jquery.geocomplete.min.js"></script>
